/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.interfaces.action.templates;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.interfaces.xmledit.XmlEditorFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

/**
 *
 * @author yuriy
 */
public class corectTemplInDbAction extends ChoiceTemplAction{
    public corectTemplInDbAction(JEditorPane __p, JEditorPane t2) {
        super(__p, t2);
        super.neededPswd = true;
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        if(! super.initAll()){
            return;
        }

        if (res.equals("__NULL")) {
            this.show();
        }
        if (res.equals("__NULL")) {
            return;
        }

        String str = "SELECT \"metaTemplates\", \"templates\", \"about\" FROM " +
                prop.getProperty("dbTableName") + " WHERE name=\'" + res + "\' " +
                "AND id=\'" + resNum + "\' ;";
        DbSelects query = super.initDbSelects();
        final XmlEditorFrame f;
        if (query.sendQuery(str)) {
            f = new XmlEditorFrame(query.getString(1, "templates"),
                    query.getString(1, "metaTemplates"), true);
            f.textAbout.setText(query.getString(1, "about"));
        }else{
            //f = new XmlEditorFrame(new FileBeginer().getTemplates(),
            //        new FileBeginer().getMetaTemplates(), true);
            JOptionPane.showMessageDialog(null, "Неудалось прочитать данные\n",
                            "error", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        f.setSaveAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String sTem = f.text1.getText().replace("\\", "\\\\").replace("\"", "\\\"").replace("\'", "\\\'");
                String sMTem = f.text2.getText().replace("\\", "\\\\").replace("\"", "\\\"").replace("\'", "\\\'");
                String sAb = f.textAbout.getText().replace("\\", "\\\\").replace("\"", "\\\"").replace("\'", "\\\'");

                DbSelects db = initDbSelects();
                String str = "UPDATE " +
                        prop.getProperty("dbTableName") +
                        " SET \"metaTemplates\"=\'" + sMTem + "\', \"templates\"=\'"
                        + sTem + "\', \"about\"=\'" +sAb + "\' " +
                        "WHERE \"id\"=\'" + resNum + "\' AND \"name\"=\'" + res + "\' ;";
                boolean b = db.sendQueryUpdate(str);
                if (b) {
                    JOptionPane.showMessageDialog(null, "Данные изменены\n",
                            "ok", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "Неудалось изменит данные\n",
                            "error", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        f.setVisible(true);

        res = "__NULL";
        resNum = 0;
    }

}
